package com.zimmur.app.mocolink.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zimmur.app.mocolink.dao.AdminRoleMapper;
import com.zimmur.app.mocolink.dao.PermissionMapper;
import com.zimmur.app.mocolink.domain.model.Permission;
import com.zimmur.app.mocolink.service.IPermissionService;

@Service
public class PermissionServiceImpl implements IPermissionService{

	@Autowired
	private PermissionMapper permissionMapper;
	@Autowired
	private AdminRoleMapper adminRoleMapper;
	@Override
	public Set<String> selectPermissionByAdminId(int adminId) {
		List<Integer> roleIds=adminRoleMapper.selectByAdminId(adminId);
		List<Permission> permissions = permissionMapper.selectByRoleId(roleIds);
		Set<String> permissionSet=new HashSet<String>();
		if(permissions!=null && permissions.size()>0){
			for(Permission permission:permissions){
				String uri=permission.getUri();
				String[] uriArray = uri.split(",");
				for(String permissionUri:uriArray){
					permissionSet.add(permissionUri);
				}
			}
		}
		return permissionSet;
	}
	@Override
	public Map<String, String> selectPermissionCodeByadmin(Integer adminId) {
		List<Integer> roleIds=adminRoleMapper.selectByAdminId(adminId);
		List<Permission> permissions = permissionMapper.selectByRoleId(roleIds);
		Map<String,String> permissionCode=new HashMap<>();
		if(permissions!=null && permissions.size()>0){
			for(Permission permission:permissions){
				permissionCode.put("P"+permission.getCode(), permission.getCode());
			}
		}
		return permissionCode;
	}
}
